<br>
<br>
<div class="row">
    <div class="col-md-12">
        <div class="grid simple">
            <div class="grid-body no-border">
                <form id="edit-priv"> 
                    <h4><?= $role['name'] ?></h4>
                    <hr>
                    <div class="row form-row">
                        <div class="col-md-12">
                            <? if($role_resource): ?>
                                <table class="table table-bordered no-more-tables">
                                    <colgroup>
                                        <col />
                                        <col width="10%" />
                                    </colgroup>
                                    <thead>
                                        <tr>
                                            <th>程序名</th>
                                            <th>操作</th>
                                            <? foreach($role_resource_group as $item): ?>
                                                <th><?= $item ?></th>
                                            <? endforeach ?>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <? foreach($role_resource as $r): ?>
                                            <tr>
                                                <td>
                                                    <?= $r['name'] ?>
                                                </td>
                                                <td>
                                                    <a href="javascript:void(0)" onclick="$.Edit_priv.select_all(this)">
                                                        全选
                                                    </a>
                                                    &nbsp;
                                                    <a href="javascript:void(0)" onclick="$.Edit_priv.select_none(this)">
                                                        反选
                                                    </a>
                                                </td>
                                                <? foreach($role_resource_group as $rg => $rgv): ?>
                                                <td>
                                                    <? $id = "PRIV-{$r['resource_id']}-{$rg}" ?>
                                                    <div class="checkbox check-success">
                                                        <? if(is_array($role['privs'])): ?>
                                                            <!-- 当前拥有的权限 -->
                                                            <input type="checkbox" <?= in_array("{$r['resource_id']}-{$rg}", $role['privs']) ? 'checked="checked"' : '' ?> value="<?= "{$r['resource_id']}-{$rg}" ?>" name="privs[]" id="<?= $id ?>">
                                                        <? else: ?>
                                                            <input type="checkbox" value="<?= "{$r['resource_id']}-{$rg}" ?>" name="privs[]" id="<?= $id ?>">
                                                        <? endif ?>
                                                        
                                                        <label for="<?= $id ?>"></label>
                                                    </div>
                                                </td>
                                                <? endforeach ?>
                                            </tr>
                                        <? endforeach ?>
                                    </tbody>
                                </table>
                            <? endif ?>
                        </div> 
                    </div>
                    <br>
                      
                    <div class="form-actions">  
                        <div class="pull-left">
                            <button class="btn btn-success btn-cons" type="button" onclick="$.Edit_priv.submit()">
                                <i class="icon-ok"></i> 保存</button>
                            </button>
                            <button class="btn btn-white btn-cons" type="button" onclick="$.T.reset_form(this)">
                                重置
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    $.Edit_priv = {
        role_id: <?= $role['role_id']?>,
        submit: function()
        {
            var form   = $('#edit-priv');
            var params = form.serializeArray();
            var flag   = false;
            for (var i in params)
            {
                if(params[i]['name'] == 'privs[]')
                {
                    flag = true;
                    break;
                }
            }
            if(flag === true)
            {
                form
                    .attr('action', $.G.domain+'role/edit_priv/'+$.Edit_priv.role_id)
                    .attr('method', 'POST')
                    .submit();
            }
            else
            {
                $.T.confirm("您没有选择任何权限，确认要删除该角色所有权限吗？", function(){
                    form
                    .attr('action', $.G.domain+'role/del_priv/'+$.Edit_priv.role_id)
                    .attr('method', 'POST')
                    .submit();
                });
            }
        },

        /**
         * [全选]
         */
        select_all: function(obj) {
            $(obj).parents('tr').find('input').attr('checked',true);
        },

        /**
         * [反选]
         */
        select_none: function(obj) {
            $(obj).parents('tr').find('input').attr('checked',false);
        },
    }
</script>